package com.itheima.mm.controller;

import com.itheima.mm.constants.Constants;
import com.itheima.mm.entity.PageResult;
import com.itheima.mm.entity.QueryPageBean;
import com.itheima.mm.entity.Result;
import com.itheima.mm.pojo.Question;
import com.itheima.mm.pojo.User;
import com.itheima.mm.service.IQuestionService;
import com.itheima.mm.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

/**
 * 包名:com.itheima.mm.controller
 * 作者:Leevi
 * 日期2020-01-16  09:09
 */
@RestController
@RequestMapping("question")
public class QuestionController {
    @Autowired
    private IQuestionService questionService;
    @RequestMapping("findBasicList")
    public Result findBasicListByPage(@RequestBody QueryPageBean queryPageBean) throws IOException {
        //1. 获取请求参数
        try {
            //2. 调用业务层的方法，进行分页查询
            PageResult pageResult = questionService.BasicListByPage(queryPageBean);

            //表示获取基础题库列表成功
            return new Result(true,"获取基础题库列表成功",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取基础题库列表失败");
        }
    }
    @RequestMapping("addBasicOrUpdateBasic")
    public Result addOrUpdateBasicQuestion(@RequestBody Question question, HttpSession session) throws IOException {
        try {
            //题目上还缺少:createDate、userId
            question.setCreateDate(DateUtils.parseDate2String(new Date()));//设置题目创建时间为当前时间
            User user = (User) session.getAttribute(Constants.MM_USER_USERNAME);
            question.setUserId(user.getId());//设置题目创建者的id为当前用户的id

            //调用业务层的方法，添加题目信息
            questionService.addOrUpdateBasicQuestion(question);
            //表示添加或者修改成功
            return new Result(true,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"操作失败");
        }
    }
}
